//
//  COTipViewController.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/31.
//

import UIKit

class COTipViewController: COBaseViewController {
    private var animationDelegate: COPresentAnimationDelegate = COPresentAnimationDelegate()
    var showVc: UIViewController?
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nil, bundle: nil)
        self.modalPresentationStyle = .custom
        self.transitioningDelegate = animationDelegate
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override var navShow: Bool {
        false
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func configUI() {
        
        view.addSubview(mainView)
        mainView.addSubview(backImageView)
        mainView.addSubviews([reportButton, lineView1, blockButton, lineView2, cancelButton])
        
        mainView.snp.makeConstraints { make in
//            make.left.right.bottom.equalToSuperview()
            make.edges.equalToSuperview()
            make.height.equalTo(262+bottomSafeHeight)
        }
        
        backImageView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(-1)
            make.bottom.top.equalToSuperview().offset(0)
        }
        
        reportButton.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(84)
        }
        
        lineView1.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(reportButton.snp.bottom).offset(0)
            make.height.equalTo(1)
        }
        
        blockButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(lineView1.snp.bottom).offset(0)
            make.height.equalTo(84)
        }
        
        lineView2.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(blockButton.snp.bottom).offset(0)
            make.height.equalTo(1)
        }
        
        cancelButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(lineView2.snp.bottom).offset(0)
            make.height.equalTo(84)
        }
    }

    private lazy var mainView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    private lazy var backImageView: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "矩形"))
        return imgV
    }()
    
    private lazy var reportButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("Report", for: .normal)
        btn.setTitleColor(.rgba(152, 48, 255, 1), for: .normal)
        btn.titleLabel?.font = .medium(20)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
    
    private lazy var lineView1: UIView = {
        let view = UIView()
        view.backgroundColor = .white.withAlphaComponent(0.15)
        return view
    }()
    
    private lazy var blockButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("Block", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .medium(20)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var lineView2: UIView = {
        let view = UIView()
        view.backgroundColor = .white.withAlphaComponent(0.15)
        return view
    }()
    
    private lazy var cancelButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("Cancel", for: .normal)
        btn.setTitleColor(.white.withAlphaComponent(0.5), for: .normal)
        btn.titleLabel?.font = .medium(20)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
}

extension COTipViewController {
    @objc
    private func clickButton(_ btn: UIButton) {
        var text = ""
        if btn == cancelButton {
            self.dismiss(animated: true)
            return
        } else if btn == reportButton {
            self.dismiss(animated: true) {
                let vc = COReportViewController()
                vc.showVc = self.showVc
                self.showVc?.present(vc, animated: true)
            }
            return
        } else {
            text = "Blocked successfully"
        }
        self.view.makeToastActivity(.center)
        
        DispatchQueue.main.asyncAfter(deadline: .now()+2, execute: {
            self.view.hideToastActivity()
            self.view.makeToast(text, position: .center) { _ in
                self.dismiss(animated: true)
            }
        })
    }
}
